<?php
// +----------------------------------------------------------------------
// | HkCms 内容模型管理
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\content\controller;

use common\controller\AdminBase;
use think\Db;

class Models extends AdminBase {

    //显示模型列表
    public function index() {

        $data = model("content/Model")->where(array("type" => 0))->select();

        $this->assign("data", $data);
        
        return $this->fetch();
    }

    //添加模型
    public function add() 
    {
        if ( request()->isPOST() ) {

            $data = input('post.');

            if (empty($data)) {

                $this->error('提交数据不能为空！');
            }

            if ( model("content/Model")->addModel($data) ) {

                cache('ModelField',NULL);
                $this->success("添加模型成功！");
            } else {

                $error = model("content/Model")->getError();
                $this->error($error ? $error : '添加失败！');
            }
        } 
        return $this->fetch();
    }

    //编辑模型
    public function edit() {
        if ( request()->isPOST() ) {
            $data = input('post.');
            if (empty($data)) {
                $this->error('提交数据不能为空！');
            }
            if (model("content/Model")->editModel($data)) {
                $this->success('模型修改成功！', url('index'));
            } else {
                $error = model("content/Model")->getError();
                $this->error($error ? $error : '修改失败！');
            }
        } else {
            $modelid = input('modelid', 0, 'intval');
            $data = model("content/Model")->where("modelid",$modelid)->find();
            $this->assign("data", $data);
            return $this->fetch();
        }
    }

    //删除模型
    public function delete() 
    {
        $modelid = input('modelid', 0, 'intval');

        //检查该模型是否已经被使用
        $count = Db::name("Category")->where(array("modelid" => $modelid))->count();

        if ($count) {

            $this->error("该模型已经在使用中，请删除栏目后再进行删除！");
        }

        //这里可以根据缓存获取表名
        $modeldata = model("content/Model")->where("modelid",$modelid)->find();

        if (!$modeldata) {

            $this->error("要删除的模型不存在！");
        }

        if (model("content/Model")->deleteModel($modelid)) {

            $this->success("删除成功！", url("index"));

        } else {

            $this->error("删除失败！");
        }
    }

    //检查表是否已经存在
    public function public_check_tablename() {
        $tablename = input('tablename', '', 'trim');
        $count = model("content/Model")->where(array("tablename" => $tablename))->count();
        if ($count == 0) {
            $this->success('表名不存在！');
        } else {
            $this->error('表名已经存在！');
        }
    }

    //模型的禁用与启用
    public function disabled() {
        $modelid = Input('modelid', 0, 'intval');
        $disabled = Input('disabled') ? 0 : 1;
        $status = model("content/Model")->where('modelid',$modelid)->update(array('disabled' => $disabled));
        if ($status !== false) {
            $this->success("操作成功！");
        } else {
            $this->error("操作失败！");
        }
    }

    //模型导入
    public function import() {

        if ( request()->isPOST() ) {

            if (empty($_FILES['file'])) {

                $this->error("请选择上传文件！");

            }

            $filename = $_FILES['file']['tmp_name'];

            if (strtolower(substr($_FILES['file']['name'], -3, 3)) != 'txt') {

                $this->error("上传的文件格式有误！");
            }

            //读取文件
            $data = file_get_contents($filename);
// dump($data);exit();
            //删除
            @unlink($filename);

            //模型名称
            $name = input('post.name', NULL, 'trim');

            //模型表键名
            $tablename = input('post.tablename', NULL, 'trim');

            $C_model = model("content/Model");

            //导入
            $status = $C_model->import($data, $tablename, $name);

            if ($status) {

                $this->success("模型导入成功，请及时更新缓存！");

            } else {
// dump($C_model->getError());exit();
                $this->error($C_model->getError() ? $C_model->getError() : '模型导入失败！');
            }
        }

        return $this->fetch();
    }

    //模型导出
    public function export() 
    {
        //需要导出的模型ID
        $modelid = input('modelid', 0, 'intval');

        if (empty($modelid)) {

            $this->error('请指定需要导出的模型！');
        }

        config('SHOW_PAGE_TRACE',false);

        //导出模型
        $status = model("content/Model")->export($modelid);

        if ($status) {

            header("Content-type: application/octet-stream");

            header("Content-Disposition: attachment; filename=spf_model_" . $modelid . '.txt');

            echo $status;

        } else {
            $this->error(model("content/Model")->getError() ? model("content/Model")->getError() : '模型导出失败！');
        }
    }

}
